package at.tripes.controller;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import at.tripes.calculator.Calculator;

@Controller
@RequestMapping("/")
public class BaseController {

    private static Logger logger = Logger.getLogger(BaseController.class);

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String welcome(ModelMap model) {
        logger.info("Controller called. root mapping called.");

        model.addAttribute("message", "You're awesome.");

        // Spring uses InternalResourceViewResolver and return back index.jsp
        return "index";

    }

    @RequestMapping(value = "/add/{number1}+{number2}", method = RequestMethod.GET)
    public String welcomeName(@PathVariable String number1, @PathVariable String number2, ModelMap model) {
        logger.info("Controller called. params are: {" + number1 + "}, {" + number2 + "}");
        Calculator calc = new Calculator();
        String result = Integer.toString(calc.calculate(Integer.parseInt(number1), Integer.parseInt(number2)));
        model.addAttribute("message", "Add " + number1 + " to " + number2 + " = " + result);
        return "index";

    }

}
